/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pkg;

import CRUD.MemberHome;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import xmlMapping.*;
import xmlMapping.Thread;
import Utility.Utility;
import beans.UserType;
import java.util.Iterator;
import java.util.List;
import javax.servlet.jsp.PageContext;

/**
 *
 * @author Mostafa
 */
public class DisplayThreadTagHandler extends SimpleTagSupport {

    Utility u=new Utility();
   
    private  int forumid;
    
    public String displayOperation()
    {
        String resString="";
        String lurkString="";
         String markString="";
         String stickString;
        if(type!=null)
        {
                  Iterator <Member> itr1=currentthread.getMembers().iterator();
             boolean ismarked=false;
             while (itr1.hasNext()) {
                 Member m = itr1.next();
                if(m.getMemberId().equals(member.getMemberId())) 
                {ismarked=true;
                break;
                }
                
            }
               if( ismarked)
                  {
                      markString="  <a href=\"marklurkoperation.do?threadoperation=removeThreadMark&threadid="+currentthread.getThreadId()+"\" > UnMark </a>  ";
                  }
                  else{
                  markString="  <a href=\"marklurkoperation.do?threadoperation=setThreadMark&threadid="+currentthread.getThreadId()+"\" >  Mark </a>  ";
                  }
               
               
               
               
             Iterator <Member> itr2=currentthread.getMembers_1().iterator();
             boolean islurked=false;
             while (itr2.hasNext()) {
                 Member m = itr2.next();
                if(m.getMemberId().equals(member.getMemberId())) 
                {islurked=true;
                break;
                }
                
            }
               
               
                    if( islurked)
                  {
                     lurkString="<a href=\"marklurkoperation.do?threadoperation=removeLurk&threadid="+currentthread.getThreadId()+"\" >UnLurk </a>";
                  }
                  else{
                   lurkString="<a href=\"marklurkoperation.do?threadoperation=setThreadLurk&threadid="+currentthread.getThreadId()+"\" >Lurk </a>";
                  }
                    
                    if(currentthread.isSticky())
                    {
                    stickString="<a href=\"stickaction.do?threadid="+currentthread.getThreadId()+ "&method=unstick\">UnStick</a> ";
                    }
                    else{
                    stickString="<a href=\"stickaction.do?threadid="+currentthread.getThreadId()+ "&method=stick\">Stick</a> ";
                    }
                  
        if(type.equals(UserType.adm))
        {
            
            
          
            
                    
                    //members_1 ...  threadlurked
                    //members  ....  threadmarked
                   
                 
                    
         resString="<a href=\"splitaction.do?method=Split_Threads&threadid="+currentthread.getThreadId()+"\">Split</a>      "+stickString
                 + "    <a href=\"editaction.do?threadid="+currentthread.getThreadId()+"&method=prepareEdit\">Edit</a>     "
                +blockTxt+"      "+lockTxt+"  <a href=\"moveaction.do?threadid="+currentthread.getThreadId()+"&method=prepareMove\">Move</a>   "   ;
    
        }
        else if(type.equals(UserType.supermod))
        {
         resString="<a href=\"splitaction.do?method=Split_Threads&threadid="+currentthread.getThreadId()+"\">Split</a>     "+stickString
                 + "   <a href=\"editaction.do?threadid="+currentthread.getThreadId()+"&method=prepareEdit\">Edit</a>     "
                +blockTxt+"        <a href=\"moveaction.do?threadid="+currentthread.getThreadId()+"&method=prepareMove\">Move</a>";
    
        }
                
        else if(type.equals(UserType.mod))
        {
       
                       boolean ispermitted=false;
                       
           Iterator<Forum> itr=member.getForums().iterator();
            while (itr.hasNext()) {
                Forum forum = itr.next();
                 System.out.println("Check......................");
                if(forum.getForumId().equals(forumid))
                {
                ispermitted=true;
                        System.out.println("Permitted......................");
               break;
                }
                
            }
        if(ispermitted)
        {
            if(moderatorpermissions.isEditthread())
            {
            resString="<a href=\"splitaction.do?method=Split_Threads&threadid="+currentthread.getThreadId()+"\">Split</a>      "+stickString
                    + "   <a href=\"editaction.do?threadid="+currentthread.getThreadId()+"&method=prepareEdit\">Edit</a>     "
                +blockTxt+"        <a href=\"moveaction.do?threadid="+currentthread.getThreadId()+"&method=prepareMove\">Move</a>";
    
            }
          
        } 
        
        }
        }
        
    resString+=markString+"   "+lurkString;
        return resString;
    }
    
    
    
    
    
    
    
    
    public String displayInput(Integer threadid)
    {
       String resString="";
        if(type!=null)
        {
        if(type.equals(UserType.adm))
        {
         resString=" <input type=\"checkbox\" name=\"cb\" value=\""+threadid+"\"/>";
        }
        else if(type.equals(UserType.supermod))
        {
         resString=" <input type=\"checkbox\" name=\"cb\" value=\""+threadid+"\"/>";
        }
                
        else if(type.equals(UserType.mod))
        {
       
          
                  boolean ispermitted=false;
           Iterator<Forum> itr=member.getForums().iterator();
            while (itr.hasNext()) {
                Forum forum = itr.next();
                 System.out.println("Check......................");
                if(forum.getForumId().equals(forumid))
                {
                ispermitted=true;
                        System.out.println("Permitted......................");
               break;
                }
                
            }
        if(ispermitted)
        {
            if(moderatorpermissions.isEditthread())
            {
            resString=" <input type=\"checkbox\" name=\"cb\" value=\""+threadid+"\"/>";
            }
          
        } 
        
        }
        }
    
        return resString;
   
    }

    
    public  String lasttxt()
    {
      boolean isedit=false;
      
        String resString="";
        if(type!=null)
        {
        if(type.equals(UserType.adm))
        {
         resString=" <div class=\"buttons\">"
                 + " <h4> <input type=\"submit\" name=\"actionType\" value=\"Merge_Threads\">"
                 + "  <input type=\"submit\" name=\"actionType\" value=\"Delete_Threads\"></h4></div>";
        }
        else if(type.equals(UserType.supermod))
        {
         resString=" <div class=\"buttons\">"
                 + " <h4> <input type=\"submit\" name=\"actionType\" value=\"Merge_Threads\">"
                 + "  <input type=\"submit\" name=\"actionType\" value=\"Delete_Threads\"></h4></div>";
        } 
                
                
        else if(type.equals(UserType.mod))
        {
       
           boolean ispermitted=false;
           Iterator<Forum> itr=member.getForums().iterator();
            while (itr.hasNext()) {
                Forum forum = itr.next();
                 System.out.println("Check......................");
                if(forum.getForumId().equals(forumid))
                {
                ispermitted=true;
                        System.out.println("Permitted......................");
               break;
                }
                
            }
           
        if(ispermitted)
        {
             System.out.println(" Execute Permitted......................");
              System.out.println("Edit is "+moderatorpermissions.isEditthread()+"......................");
               System.out.println("Delete is "+moderatorpermissions.isDeletethread()+"......................");
              
            if(moderatorpermissions.isEditthread())
            {
            resString=" <div class=\"buttons\">"
                 + " <h4> <input type=\"submit\" name=\"actionType\" value=\"Merge_Threads\" /> ";
            isedit=true;
                 
           
            }
            if(moderatorpermissions.isDeletethread()){resString+="  <input type=\"submit\" name=\"actionType\" value=\"Delete_Thread\" />";}
            resString+="";
            
          
        } 
        
        }
        }
        if(!isedit) resString+="<h4>";
  resString+=" <input type=\"submit\" name=\"actionType\" value=\"Add_Thread\" /></h4> </div>";
  //
        return resString;
    
        
    }
    
    public void setForumid(int forumid) {
        this.forumid = forumid;
    }

   

    String StickTxT="<div class=\"forabg\"><h1></h1> <div class=\"inner\"><span class=\"corners-top\"><span></span></span>"
	+"<ul class=\"topiclist\"><li class=\"header\"><dl class=\"icon\">"
        +" <dt> Sticky Topics &nbsp;"
         +" </dt><dd class=\"topics\"></dd><dd class=\"posts\">Posts</dd><dd class=\"lastpost\"><span>Last post</span></dd>"
         +"</dl></li></ul><ul class=\"topiclist forums\"> ";
      
    
    
     String RegularTxT="<div class=\"forabg\"><h1></h1> <div class=\"inner\"><span class=\"corners-top\"><span></span></span>"
	+"<ul class=\"topiclist\"><li class=\"header\"><dl class=\"icon\">"
        +" <dt> Forum Topics "
         +" </dt><dd class=\"topics\"></dd><dd class=\"posts\">Posts</dd><dd class=\"lastpost\"><span>Last post</span></dd>"
         +"</dl></li></ul><ul class=\"topiclist forums\"> ";
      
    
    
      String tailTxT="</ul></div></div>";
     
     
    
  
    
    Member member;
    String blockTxt;
    String lockTxt;
    Moderatorpermessions moderatorpermissions;
    UserType type;
    Thread currentthread;
    
    
    /**
     * Called by the container to invoke this tag. 
     * The implementation of this method is provided by the tag library developer,
     * and handles all tag processing, body iteration, etc.
     */
    @Override
    public void doTag() throws JspException {
        MemberHome mh=new MemberHome();
        JspWriter out = getJspContext().getOut();
          type= (UserType)getJspContext().getAttribute("type",PageContext.SESSION_SCOPE);
          Integer id=(Integer)getJspContext().getAttribute("id",PageContext.SESSION_SCOPE);
          if(id!=null){
          member= mh.findById(id);
       if(type.equals(UserType.mod))
       { moderatorpermissions=member.getModeratorpermessions();}
          }
          
        
     
         List fs= u.getThreads(forumid);
        
        
        
        try {
           
            String result="";
            Iterator it=fs.iterator();
            
            while (it.hasNext()) {                
                
            
     Thread t=(Thread)it.next();
     currentthread=t;
       blockTxt="<a href=\"blockaction.do?method=block&threadid="+t.getThreadId()+"\">Block</a>";
             lockTxt="<a href=\"admguest2.do?actiontype=lockthread&id="+t.getThreadId()+"\">Lock</a>";
                  
     if(t.isBlocked())
     {blockTxt="<a href=\"blockaction.do?method=unblock&threadid="+t.getThreadId()+"\">UnBlock</a>";}
     if(t.isLocked())
     {lockTxt="<a href=\"admguest2.do?actiontype=lockthread&unlock=1&id="+t.getThreadId()+"\">Unlock</a>";}
             
     
     String lastposttxt="";
     if(t.getPost()!=null)lastposttxt+= "by <a href=\"memberlist.html\">"+t.getPost().getMember().getUserName()+"</a>"
   + "	<a href=\"ViewTopic.jsp\"><img src=\"imageset/icon_topic_latest.gif\" width=\"11\" height=\"9\" alt=\"View the latest post\" title=\"View the latest post\" />"
   + "</a> <br />"+t.getPost().getLastModified();
     else lastposttxt+="No Post Added Yet";
             
     
     
               if(t.isSticky())
               {
                   

                  StickTxT+="<li class=\"row\"><dd> &nbsp;&nbsp;"+displayInput(t.getThreadId())+" </dd><dl class=\"icon\" style=\"background-image: url(./styles/carbon_fiber/imageset/forum_read.png); background-repeat: no-repeat;\">"
                    + "<dt title=\"No new posts\"><a href=\"thread.do?pagenumber=1&threadid="+t.getThreadId()+"&forumId="+forumid+"\" class=\"forumtitle\">"+t.getTitle() +"</a><br />"
                    + t.getThread_description()+"</dt><dd class=\"privi\">"+displayOperation() +"<dfn></dfn></dd><dd class=\"posts\">"+t.getNoofpost()+" <dfn>Posts</dfn></dd>"
                    + "<dd class=\\lastpost\"><span><dfn>Last post</dfn>"+lastposttxt+"</span></dd></dl></li>";
            
               }
               else{
               
           RegularTxT+="<li class=\"row\"><dd> &nbsp;&nbsp;"+displayInput(t.getThreadId())+" </dd><dl class=\"icon\" style=\"background-image: url(./styles/carbon_fiber/imageset/forum_read.png); background-repeat: no-repeat;\">"
                    + "<dt title=\"No new posts\"> <a href=\"thread.do?pagenumber=1&threadid="+t.getThreadId()+"&forumId="+forumid+"\"  class=\"forumtitle\">"+t.getTitle() +"</a><br />"
                    + t.getThread_description()+"</dt><dd class=\"privi\">"+displayOperation() +"<dfn></dfn></dd><dd class=\"posts\">"+t.getNoofpost()+" <dfn>Posts</dfn></dd>"
                    + "<dd class=\\lastpost\"><span><dfn>Last post</dfn>"+lastposttxt+"</span></dd></dl></li>";
           
                   
               }
                
            }
          
           StickTxT+=tailTxT;
           out.println(StickTxT);
           RegularTxT+=tailTxT;
           RegularTxT+=lasttxt();
           out.println(RegularTxT);
           
           
           
           
        } catch (java.io.IOException ex) {
           
        ex.printStackTrace();
        }
    }
}
